<?php
class UsersController extends AppController {
	public $name = "Users";
	public $helpers = array (
		"Html",
		"Form",
		"Cache"
	);
	public $cacheAction = array (
		"index",
		"login",
		"rigester"
	);
	function checkSession() {
		// If the session info hasn't been set...
		if (!$this->Session->check('User')) {
			// Force the user to login
			$this->redirect('/users/login');
			exit ();
		}
	}
	/**
	 * Do some configuration settings here before filting the data
	 */
	public function beforeFilter() {
		Configure :: write('Config.language', $this->Session->read('Config.language'));
		Configure :: write('Config.language', 'chi');
		$locale = Configure :: read('Config.language');
		$this->Session->write('Config.language', $locale);
		/**
		 * load the ctp files according to the language configuration.
		 */
		if ($locale && file_exists(APP . "view" . DS . $locale . DS . $this->viewPath)) {
			$this->viewPath = $locale . DS . $this->viewPath;
		}
	}

	function index() {
		$this->set("message", "Nothing has been done!");
	}
	function login() {
		//Don't show the error message if no data has been submitted.
		$this->set('error', false);

		// If a user has submitted form data:
		if (!empty ($this->data)) {
			// First, let's see if there are any users in the database
			// with the username supplied by the user using the form:

			$someone = $this->User->findByUsername($this->data['User']['username']);

			// At this point, $someone is full of user data, or its empty.
			// Let's compare the form-submitted password with the one in
			// the database.

			if (!empty ($someone['User']['password']) && $someone['User']['password'] == $this->data['User']['password']) {
				// Note: hopefully your password in the DB is hashed,
				// so your comparison might look more like:
				// md5($this->data['User']['password']) == ...

				// This means they were the same. We can now build some basic
				// session information to remember this user as 'logged-in'.

				$this->Session->write('User', $someone['User']);

				// Now that we have them stored in a session, forward them on
				// to a landing page for the application.

				$this->redirect('/clients');
			}
			// Else, they supplied incorrect data:
			else {
				// Remember the $error var in the view? Let's set that to true:
				$this->set('error', true);
				$this->set('errorinfo', __('You are wrong input', "高手"));
			}
		}
	}

	function rigester() {
		if (!empty ($this->data['User'])) {
			$this->User->save($this->data['User']);
			$this->set("message", "You have rigestered success.");
		}
	}

	function logout() {
		// Redirect users to this action if they click on a Logout button.
		// All we need to do here is trash the session information:

		$this->Session->delete('User');

		// And we should probably forward them somewhere, too...

		$this->redirect('/');
	}

	public function getpass() {

	}

	function fileUpload() {
		if (!empty ($this->data)) {
			$file = $this->params['form']['image']; //获得file中上传文件的所有数据
			if (preg_match('/\\.(gif|jpe?g|png|bmp)$/i', $file['name'])) //判断文件的格式，是否符合要求
				{
				//上传图片           旧位置                新位置
				if (move_uploaded_file($file['tmp_name'], "img/test/" . $file['name'])) {
					$this->data['picture']['pic_newname'] = $file['name']; //将图片信息，放入data数组
					$this->data['picture']['pic_time'] = date("Y-m-d H:i:s"); //将时间   放入data数组
					$t = $this->Picture->save($this->data['picture']); //将data数组里的数值插入到picture模版指定的表中(picture)
					//生成缩略图
					$filex = "img/test/" . $file['name']; //指定图片路径,
					list ($width, $height) = getimagesize($filex); //获得路径中图片的长和宽数据
					$modwidth = 170;
					$modheight = 125;
					$tn = imagecreatetruecolor($modwidth, $modheight); //建立真彩色画布
					$image = imagecreatefromjpeg($filex); //建立jpg格式图象画布
					imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height); //复制出生成的所略图
					imagejpeg($tn, "img/pic/" . $file['name'], 100); //生成后一个位置
					if ($t) {
						$this->flash('图片上传/数据添加成功', '/pictures/');
					}
				}
			}
		}
	}
}
?>
